﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TasksUI.Controls
{
    public partial class SimpleTaskList : UserControl
    {
        public SimpleTaskList()
        {
            InitializeComponent();
        }

        private List<TasksModel.Task> _taskList;

        [DefaultValue(null)]
        public List<TasksModel.Task> TaskList
        {
            [System.Diagnostics.DebuggerStepThrough]
            get { return _taskList; }
            set
            {
                if (value == null)
                    return;
                _taskList = value;
                RefreshView();
            }
        }

        private void RefreshView()
        {
            if (Tag != null)
                return;// sentinel
            Tag = this;
            try
            {
                SuspendLayout();
                Controls.Clear();
                for (int i = 0; i < _taskList.Count; i++)
                {
                    TasksModel.Task t = _taskList[i];
                    AddTask(t);
                }
                ResumeLayout(true);
            }
            finally
            {
                Tag = null;
            }
        }

        public SimpleTaskControl AddTask(TasksModel.Task task)
        {
            Tag = this;
            try
            {
                SimpleTaskControl tc = new SimpleTaskControl();
                tc.Task = task;
                tc.TabIndex = _taskList.Count;
                tc.Dock = DockStyle.Top;
                // controls are sorted in reverse order
                Controls.Add(tc);
                Controls.SetChildIndex(tc, 0);
                return tc;
            }
            finally
            {
                Tag = null;
            }
        }
    }
}
